package com.db.compare.service.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSONObject;
import com.db.compare.exp.RbacMenuFilterExp;
import com.db.compare.model.RbacMenuFilter;
import com.db.compare.mapper.RbacMenuFilterMapper;
import com.db.compare.service.RbacMenuFilterService;
import com.db.compare.vo.RbacMenuFilterVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class RbacMenuFilterServiceImpl implements RbacMenuFilterService {
    @Autowired
    private RbacMenuFilterMapper rbacmenufilterMapper;
    @Override
    public PageInfo<RbacMenuFilter> pageInfo(RbacMenuFilterVo rbacmenufilterVo) {
        PageHelper.startPage(rbacmenufilterVo.getPageNum(),rbacmenufilterVo.getPageSize());
        List<RbacMenuFilter> rbacmenufilterList = rbacmenufilterMapper.findList(rbacmenufilterVo);
        PageInfo<RbacMenuFilter> pageInfo = new PageInfo<>(rbacmenufilterList);
        return pageInfo;
    }

    @Override
    public void download(String ids, HttpServletResponse response) throws Exception {
        String fileName = "过滤方式管理";
        try {
            ServletOutputStream outputStream=Appcontext.downloadResponse(response,fileName);
            if(StringUtils.isBlank(ids)){
                EasyExcel.write(outputStream, RbacMenuFilterExp.class).sheet(fileName).doWrite(null);
            }else{
                String[] arrays = ids.split(",");
                List<RbacMenuFilter> rbacmenufilterList = rbacmenufilterMapper.findByIds(arrays);
                List<RbacMenuFilterExp> rbacmenufilterExpList = new ArrayList<>();
                for(RbacMenuFilter rbacmenufilter:rbacmenufilterList){
                    RbacMenuFilterExp rbacmenufilterExp = new RbacMenuFilterExp();
                    BeanUtils.copyProperties(rbacmenufilter,rbacmenufilterExp);
                    rbacmenufilterExpList.add(rbacmenufilterExp);
                }
                EasyExcel.write(outputStream, RbacMenuFilterExp.class).sheet(fileName).doWrite(rbacmenufilterExpList);
            }
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean upload(MultipartFile file) {
        ImportParams importParams = new ImportParams();
        try {
            ExcelImportResult<RbacMenuFilterExp> result = ExcelImportUtil.importExcelMore(file.getInputStream(), RbacMenuFilterExp.class,
                    importParams);
            List<RbacMenuFilterExp> rbacmenufilterExps = result.getList();
            for (RbacMenuFilterExp rbacmenufilterExp : rbacmenufilterExps) {
                RbacMenuFilter rbacmenufilter = new RbacMenuFilter();
                BeanUtils.copyProperties(rbacmenufilter,rbacmenufilterExp);
                int i = rbacmenufilterMapper.insert(rbacmenufilter);
                if(i < 1){
                    return false;
                }
            }
        } catch (Exception e) {
            return false;
        }
        return true;
    }
}
